%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

function tracks = check_merge( i, tracks, cc )
    current_cc = [];
    track_idx = [];
    for j = 1 : length( tracks )
        %create list of regions for incomplete tracks
        if tracks(j).complete == -1;
            current_cc( end + 1 ) = tracks(j).cc_label(end);
            track_idx( end + 1 ) = j;
        end
    end
    
    %check to see if multiple tracks are connected to the same region
    cc_unique = unique( current_cc );
    cc_count = histc( current_cc, cc_unique);
    cc_unique(cc_count == 1) = [];
    
    for j = cc_unique
        merge = track_idx;
        merge(current_cc ~= j) = [];
        
        %check area and centroid bounding box to confirm merger
        %Area
        curr_feat = regionprops( cc{i+1}, 'Centroid', 'Area' );
        prev_feat = regionprops( cc{i}, 'Centroid', 'Area' );

        curr_area = curr_feat( tracks( merge( 1 )).cc_label( end )).Area;
        prev_area = [];
        for k = merge
            prev_area( end + 1 ) = prev_feat( tracks(k).cc_label( end - 1 )).Area;
        end
        t_prev_area = sum( prev_area );
        
        a_range = .2;   %percent deviation for area
        area_check = 0;
        cen_check = 0;
        if ((( 1 - a_range ) * t_prev_area ) < curr_area) && (curr_area < (( 1 + a_range ) * t_prev_area ))
            area_check = 1;
        end
        
        %Centroid
        if area_check == 1
            curr_cen = curr_feat( tracks( merge( 1 )).cc_label( end )).Centroid;
            prev_cen = [];
            for k = merge
                prev_cen( end + 1, : ) = prev_feat( tracks(k).cc_label( end - 1 )).Centroid;
            end
            
            b = bounding_box_points( prev_cen );
            
            if curr_cen(2) > b(1) && curr_cen(2) < b(2) && curr_cen(1) > b(3) && curr_cen(1) < b(4)
                cen_check = 1;
            end
        end
        
        %if merge conditions are met, mark merged tracks and frame in which
        %they merged.
        
        if area_check == 1 && cen_check == 1
            %track with largest area will be continued as the merged track
            [~,largest_track] = max( prev_area );
            
            for k = merge
                if k ~= merge(largest_track);
                    tracks(k).cc_label(end) = [];
                    tracks(k).complete = 0;
                end
                
                tracks(k).tracks_merged = merge;
                tracks(k).merge_frame = i + 1;
            end
        end
    end
